﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Housing
{
    public class FinnalyCheck
    {
        private int _fin_id;
        private int _fin_noid;
        private string _fin_checktime;
        private string _fin_signature;
        private string _fin_responame;
        private string _fin_applyid;

        public string Fin_applyid
        {
            get { return _fin_applyid; }
            set { _fin_applyid = value; }
        }
        public int fin_id
        {
            get { return _fin_id; }
            set { _fin_id = value; }
        }
        public int fin_noid
        {
            get { return _fin_noid; }
            set { _fin_noid = value; }
        }
        public string fin_checktime
        {
            get { return _fin_checktime; }
            set { _fin_checktime = value; }
        }
        public string fin_signature
        {
            get { return _fin_signature; }
            set { _fin_signature = value; }
        }
        public string fin_responame
        {
            get { return _fin_responame; }
            set { _fin_responame = value; }
        }

        public FinnalyCheck()
        {
        }

        public FinnalyCheck(int finId)
        {
            GetFinnalyCheckByfinId(finId);
        }

        public void GetFinnalyCheckByfinId(int finId)
        {
            string query = "SELECT * FROM FinnalyCheck WHERE fin_id=" + finId;

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, query);

            if (dr.Read())
            {
                _fin_id = (int)dr[0];
                _fin_noid = (int)dr[1];
                _fin_checktime = Utils.GetString(dr[2]);
                _fin_signature = Utils.GetString(dr[3]);
                _fin_responame = Utils.GetString(dr[4]);
            }

            dr.Close();
        }

        public int AddNewFinnalyCheck(FinnalyCheck fin)
        {
            string query = "INSERT INTO FinallyCheck(fin_noid, fin_checktime, fin_signature, fin_responsname,applyid)"
                + "VALUES(" + fin.fin_noid + ",'" + fin.fin_checktime + "','" + fin.fin_signature + "','" + fin.fin_responame + "',"+fin.Fin_applyid+")";

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int UpdateFinnalyCheckByfinId(FinnalyCheck fin)
        {
            string query = "UPDATE FinnalyCheck SET fin_noid=" + fin.fin_noid + ",fin_checktimg='" + fin.fin_checktime + "',"
                + "fin_signature='" + fin.fin_signature + "', fin_responsname='" + fin.fin_responame + "'"
                + "WHERE fin_id=" + fin.fin_id;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int DeleteFinnalyCheckByfinId(int finId)
        {
            string query = "DELETE FROM FinnalyCheck WHERE fin_id=" + finId;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public bool AddFinCheck(string applyid, string name, string responsename, string date)
        {
            string cmdtext = "SELECT COUNT(*) FROM FinallyCheck WHERE applyid=" + applyid;
            int succornot = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, cmdtext));
            if (succornot == 0)
            {
                FinnalyCheck finnalycheck = new FinnalyCheck();
                finnalycheck._fin_checktime = date;
                finnalycheck._fin_noid = 0;
                finnalycheck._fin_responame = responsename;
                finnalycheck._fin_signature = name;
                finnalycheck._fin_applyid = applyid;
                bool succ=Convert.ToBoolean(finnalycheck.AddNewFinnalyCheck(finnalycheck));
                return succ;
            }
            else
                return false;
        }
    }
}